package array;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Matrix {
    /**
     * 旋转矩阵,旋转90度
     */
    public void rotate(int[][] matrix) {
        int[][] matrixCopy = new int[matrix.length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                matrixCopy[i][j] = matrix[i][j];
            }
        }

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                matrix[i][matrix.length - 1 - j] = matrixCopy[j][i];
            }
        }
    }

    /**
     * 零矩阵:若元素为0,行与列清零
     */
    public void setZeroes(int[][] matrix) {
        Set<Integer> set = new HashSet<>();
        int index = 0;
        boolean row_clean;
        for (int i = 0; i < matrix.length; i++) {
            row_clean = false;
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == 0) {
                    set.add(j);
                    index++;
                    row_clean = true;
                }
            }


        }

        for (int i = 0; i < matrix.length; i++) {
            for (int j : set) {
                matrix[i][j] = 0;
                matrix[j][i] = 0;
            }
        }
    }

    /**
     * 对角线遍历
     */

    public int[] findDiagonalOrder(int[][] mat) {
        int[] arr = new int[(int) Math.pow(mat.length, 2)];
        //往右或往下, 如果超出, 则往下或往右
        //time走完一个循环+1,奇数往右/超出范围往下, 偶数往下/超出范围往右
        //走的长度为index,如果index >=length, 长度为time--;
        int time = 1;
        int index = 0;
        int col = 0;
        int row = 0;
        while (time < mat.length * 2) {
            if (time % 2 == 1) {
                while (row - 1 >= 0 && col + 1 <= mat.length - 1) {
                    arr[index++] = mat[row][col];
                    row -= 1;
                    col += 1;
                }
                //往右上
                if (col + 1 > mat.length - 1) {
                    //往下
                    arr[index++] = mat[row][col];
                    row++;
                } else if (row - 1 < 0) {
                    //超出范围就往右走一步
                    arr[index++] = mat[row][col];
                    col++;
                }
                time++;
            } else {
                while (col - 1 >= 0 && row + 1 <= mat.length - 1) {
                    arr[index++] = mat[row][col];
                    row += 1;
                    col -= 1;
                }
                //左下
                if (row + 1 > mat.length - 1) {
                    arr[index++] = mat[row][col];
                    col++;
                } else if (col - 1 < 0) {
                    arr[index++] = mat[row][col];
                    row++;
                }
                time++;
            }
        }
        return arr;
    }

    /**
     * 螺旋矩阵II
     */

    public int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        int index = 1, i = 0, j = 0;
        int left = 0, right = n - 1, up = 1,down = n - 1;
        int director = 0;

        /*
          director = 右 0,下 1,左 2,上 3
          移动后,判断下一步该往哪走--定义边界
         */
        while (index <= n * n) {
            matrix[i][j] = index;
            switch (director) {
                case 0:
                    j++;
                    if(j+1>right){
                        //如果到右边界
                        director = 1;
                        right--;
                    }
                    break;
                case 1:
                    i++;
                    if(i+1>down){
                        director = 2;
                        down--;
                    }
                    break;
                case 2:
                    j--;
                    if(j-1<left){
                        director = 3;
                        left++;
                    }
                    break;
                case 3:
                    i--;
                    if(i-1<up){
                        director = 0;
                        up++;
                    }
                    break;
            }
            index++;
        }
        return matrix;
    }
}
